<?php

namespace App\Services\User;


use App\Constants\CodeData;
use App\Exception\MessageException;
use App\Model\GreetMaster;
use App\Utils\RedisClient;
use function Hyperf\Translation\trans;

class GreetMasterService
{

    /**
     * Note - 女用户获取打招呼列表
     * DateTime 2024/8/20 11:45
     * User Admin
     * @param mixed $userInfo
     * @param $is_choose
     * @return array
     */
    public function getGreetingList(mixed $userInfo, $is_choose)
    {
        $uid = $userInfo['id'];
        // 如果是男性
        if ($userInfo['sex'] == 1) {
            throw new MessageException('网络错误');
        }

        $is_change = 0;
        if ($is_choose) {
            // $where = "uid=? and is_choose=1 and status=1";
            $redis = RedisClient::getInstance();
            $list = $redis->get("greet_master_data:$uid");
            if (!$list) {
                // $list = (new GreetMasterModel())->findAll("id master_id,is_choose, uid, audio_url, audio_len, text, image_url, `status`, remark", $where, [$uid]);
                $list = GreetMaster::query()
                    ->selectRaw('id as master_id,is_choose, uid, audio_url, audio_len, text, image_url, status, remark, w, h')
                    ->where('uid', $uid)
                    ->where('is_choose', 1)
                    ->where('status', 1)
                    ->get()->toArray();
                $is_change = 1;
                $redis->set("greet_master_data:$uid", json_encode($list), 24 * 60 * 60);
            } else {
                $list = json_decode($list, true);
            }
        } else {
            // $list = (new GreetMasterModel())->findAll("id master_id,is_choose, uid, audio_url, audio_len, text, image_url, `status`, remark", $where, [$uid]);
            $list = GreetMaster::query()
                ->selectRaw('id as master_id,is_choose, uid, audio_url, audio_len, text, image_url, status, remark')
                ->where('uid', $uid)
                ->get()->toArray();
        }


        // Tool::JSON(\CodeData::SUCCESS, ["result" => 1, "msg" => $list, 'is_change' => $is_change], 'success');
        return formatData(["result" => 1, "msg" => $list, 'is_change' => $is_change], 200, CodeData::SUCCESS);

    }


    /**
     * Note - 女用户创建、编辑打招呼
     * DateTime 2024/8/20 12:12
     * User Admin
     * @param mixed $uid
     * @param mixed $params
     * @return array
     */
    public function edit(mixed $uid, mixed $params)
    {
        $audio_url = $params['audio_url'] ?? '';
        $audio_len = $params['audio_len'] ?? '';
        $image_url = $params['image_url'] ?? '';
        $w = $params['w'] ?? 0;
        $h = $params['h'] ?? 0;
        $text = $params['text'] ?? '';
        $remark = $params['remark'] ?? '';
        if ($remark == '') {
            $count = GreetMaster::query()->where('uid', $uid)->count();
            $remark = trans("message.我的模板") . '- ' . ($count + 1);
        }
        $audio_len = $audio_len === "" ? 0 : $audio_len;
        $is_choose = $params['is_choose'] ?: 0;
        $master_id = $params['master_id'] ?? 0;
        if (!$master_id) {
            $create = [
                "uid" => $uid,
                "text" => $text,
                "audio_url" => $audio_url,
                "audio_len" => $audio_len,
                "image_url" => $image_url,
                "w" => $w,
                "h" => $h,
                "remark" => $remark,
                "is_choose" => $is_choose,
                "create_time" => date("Y-m-d H:i:s"),
            ];

            $rs = GreetMaster::query()->insert($create);

        } else {
            // $master=(new GreetMasterModel())->find("*",'id=?',[$master_id]);
            $master = GreetMaster::query()->where('id', $master_id)->where('uid', $uid)->first();
            if (empty($master)) {
                throw new MessageException("数据不存在");
            }
            $status = 0;
            if ($master['text'] == $text && $master['audio_url'] == $audio_url && $master['image_url'] == $image_url && $master['remark'] != $remark) {
                // 仅改变备注，状态不变
                $status = $master['status'];
            }

            $update = [
                "text" => $text,
                "audio_url" => $audio_url,
                "audio_len" => $audio_len,
                "image_url" => $image_url,
                "w" => $w,
                "h" => $h,
                "remark" => $remark,
                "status" => $status,
                "is_choose" => $is_choose,
                "update_time" => date("Y-m-d H:i:s"),
            ];
            $rs = GreetMaster::query()->where('id', $master_id)->where('uid', $uid)->update($update);
        }

        if (!$rs) {
            throw new MessageException("操作失败");
        }
        // 删除模板缓存
        RedisClient::getInstance()->del("greet_master_data:$uid");
        return formatData(array('result' => 1));
    }


    /**
     * Note - 女用户设置快捷打招呼模板
     * DateTime 2024/8/20 14:20
     * User Admin
     * @param mixed $uid
     * @param mixed $ids
     * @return array
     */
    public function settingGreet(mixed $uid, mixed $ids)
    {
        $res = GreetMaster::query()->where('uid', $uid)->update(['is_choose' => 0]);
        if (!empty($ids)) {
            $ida = explode(',', $ids);
            $res = GreetMaster::query()->where('uid', $uid)->whereIn('id', $ida)->update(['is_choose' => 1]);
        }
        RedisClient::getInstance()->del("greet_master_data:$uid");
        return formatData(['result' => 1, 'msg' => $res]);
    }


}